# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = "restructuredtext en"
__all__ = []

from ... import hint
from ... import pilelib as stack
from ...forms import gypsy as GypsyLayout
from ...gamelib import info as gi, game
from ...packlib import info as cs, place
from ...piles import talon as tstack2, tarock as tstack
from ...utillib.misc import kwdefault

Wrap = stack.StackWrapper
GT, SL = gi.GT, gi.SL


class FifteenPlus(cs.Tarock_Deck, game.Game):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.gypsy.GypsyLayout
        - row [16 over 2 row W, vert offset]
        - talon [1 SE, no offset]
        - foundation [5 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon
        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpAC_Rowstack(max_move=1, max_accept=1)
        Init all, min 4 card. Build down in rank by alt color and trump by
        rank alone. Play top.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=161, name="Fifteen plus", category=cs.TYPE_TAROCK,
                    game_type=GT.BAKERS_DOZEN, decks=1,
                    skill_level=SL.BALANCED, version='4.00')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _layout_ = GypsyLayout.GypsyLayout
    _talon_ = stack.InitialDealTalon
    _foundation_ = stack.SS_Foundation
    _row_ = Wrap(tstack.TrumpAC_RowStack, max_move=1, max_accept=1)
    _hint_ = hint.CautiousDefaultHint

    def _create_game(self, **kw):
        kwdefault(kw, rows=16, npiles=8, playcards=20)
        return super(FifteenPlus, self)._create_game(**kw)

    def _start(self, rows=5, flip=False, **kw):
        """
        Deal initial cards for new game.
          - s[rows] ~5 ea top up.

        .. \_start([row=5, flip=False]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(flip=flip, frames=0)
        self.s['talon'].dealRowAvail()


class Excuse(FifteenPlus):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.gypsy.GypsyLayout
        - row [16 over 2 row W, vert offset]
        - talon [1 SE, no offset]
        - foundation [5 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon
        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stack.RK_RowStack(max_move=1, max_accept=1,
           base_rank=cs.GAP)
        Init all open, min 4 card, with king and high trump as base. Build
        down in rank alone. Play top. Base none.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=162, name="Excuse", category=cs.TYPE_TAROCK,
                    game_type=(GT.BAKERS_DOZEN | GT.OPEN), decks=1,
                    skill_level=SL.BALANCED, version='4.00')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _row_ = Wrap(stack.RK_RowStack, max_move=1, max_accept=1,
                 base_rank=cs.GAP)

    def _pack_place(self, cards):
        """
        _pack_place(cards:seq(Card ...)) -> seq(Card ...)

        Overide to return KING as base _row_ deal.
        """
        return place.placeBaseCard(cards, len(self.s['rows']))

    def _start(self, **kw):
        """
        Deal initial cards for new game.
          - s[rows] ~5 ea up.

        .. \_start([row=5, flip=true]) -> None
        """
        kw.setdefault('flip', True)
        super(Excuse, self)._start(**kw)


class Cavalier(cs.Tarock_Deck, game.Game):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.gypsy.GypsyLayout
        - row [18 over 2 row W, vert offset]
        - talon [1 SE, no offset]
        - foundation [5 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon
        No redeal.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpAC_Rowstack(max_move=1, max_accept=1)
        Init all alt open, min 4 card. Build down in rank by alt color and
        trump by rank alone. Play top.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=202, name="Cavalier", category=cs.TYPE_TAROCK,
                    game_type=GT.BAKERS_DOZEN, decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='4.60')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _layout_ = GypsyLayout.GypsyLayout
    _talon_ = stack.InitialDealTalon
    _foundation_ = stack.SS_Foundation
    _row_ = Wrap(tstack.TrumpAC_RowStack, max_move=1, max_accept=1)

    def _create_game(self, **kw):
        kwdefault(kw, rows=18, ncols=9, playcards=19)
        return super(Cavalier, self)._create_game(**kw)

    def _start(self, flips=(False, True, False), **kw):
        """
        Deal initial cards for new game.
          - s[rows] ~5 ea alt.

        .. \_start([flips=(False, True, False)]) -> None
        """
        for flip in flips:
            self.s['talon'].dealRow(flip=flip, frames=0)
        self.s['talon'].dealRow()
        self.s['talon'].dealRowAvail()


class FiveAces(Cavalier):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.gypsy.GypsyLayout
        - row [18 over 2 row W, vert offset]
        - talon [1 SE, no offset]
        - foundation [5 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon
        No redeal.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Init base ace and low trump. Build up in rank by same suit. Play
        top. Base ace or low trump

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpAC_Rowstack(max_move=1, max_accept=1)
        Init all alt open, min 4 card. Build down in rank by alt color and
        trump by rank alone. Play top.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=203, name="Five Aces", category=cs.TYPE_TAROCK,
                    game_type=GT.BAKERS_DOZEN, decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='4.60')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _foundation_ = stack.SS_Foundation

    def _pack_place(self, cards):
        """
        _pack_place(cards:seq(Card ...)) -> seq(Card ...)

        Overide to return ACE on top by suit, deck for _foundation_ deal.
        """
        return place.MoveToTop(cards, place.AllAce)

    def _start(self, **kw):
        """
        Deal initial cards for new game.
          - s[foundations] 1 ea up.
          - s[rows] ~5 ea alt.

        .. \_start([flips=(False, True, False)]) -> None
        """
        self.s['talon'].dealRow(rows=self.s['foundations'])
        super(FiveAces, self)._start(**kw)


class Wicked(FiveAces):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.gypsy.GypsyLayout
        - row [18 over 2 row W, vert offset]
        - talon [1 SE, no offset]
        - foundation [5 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stacks.talon.Cruel_Talon (max_rounds=INFINITE)
        Redeal per row pile up to 4 card. Infinite redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Init base ace and low trump. Build up in rank by same suit. Play
        top. Base ace or low trump

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpAC_Rowstack(max_move=1, max_accept=1)
        Init all open, min 4 card. Build down in rank by same suit. Play top.
        Base none.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=204, name="Wicked", category=cs.TYPE_TAROCK,
                    game_type=(GT.BAKERS_DOZEN | GT.OPEN), decks=1,
                    redeals=cs.INFINITE, skill_level=SL.BALANCED,
                    version='4.60')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _talon_ = Wrap(tstack2.Cruel_Talon, max_rounds=cs.INFINITE)
    _row_ = Wrap(stack.SS_RowStack, max_move=1, max_accept=1,
                 base_rank=cs.GAP)
    _hint_ = hint.CautiousDefaultHint

    def _start(self, flips=(True,) * 3, **kw):
        """
        Deal initial cards for new game.
          - s[foundations] 1 ea up.
          - s[rows] ~5 ea up.

        .. \_start([flips=(True, ) * 3]) -> None
        """
        super(Wicked, self)._start(flips=flips, **kw)


class Nasty(Wicked):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.gypsy.GypsyLayout
        - row [18 over 2 row W, vert offset]
        - talon [1 SE, no offset]
        - foundation [5 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stacks.talon.Cruel_Talon (max_rounds=INFINITE)
        Redeal per row pile up to 4 card. Infinite redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Init ace and low trump. Build up in rank by same suit. Play
        top. Base ace or low trump

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpAC_Rowstack(max_move=1, max_accept=1)
        Init all open, min 4 card. Build down in rank by same suit. Play top.
        Base king or high trump.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=205, name="Nasty", category=cs.TYPE_TAROCK,
                    game_type=(GT.BAKERS_DOZEN | GT.OPEN), decks=1,
                    redeals=cs.INFINITE, skill_level=SL.BALANCED,
                    version='4.60')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _row_ = Wrap(tstack.HighSS_RowStack, max_move=1, max_accept=1)


PROVIDES = [FifteenPlus, Excuse, Cavalier, FiveAces, Wicked, Nasty]